package come.demo.widgets.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

import come.demo.widgets.BasePageAdapter;
import come.demo.widgets.BaseRecyclerAdapter;
import come.demo.widgets.R;
import tzy.refreshlayout.CustomRefreshLayout;

public class RefreshActivity extends AppCompatActivity implements CustomRefreshLayout.OnRefreshLoadListener {

    private ViewPager mViewPager;
    private TabLayout mTabLayout;
    private CustomRefreshLayout mRefreshLayout;
    private MyListFragment[] mFragments;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_refresh);
        mViewPager = findViewById(R.id.viewpager);
        mFragments = new MyListFragment[3];
        for (int i = 0; i < mFragments.length; ++i) {
            mFragments[i] = new MyListFragment();
        }
        mViewPager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager(), mFragments));
        mTabLayout = findViewById(R.id.tablayout);
        mTabLayout.setupWithViewPager(mViewPager);
        mRefreshLayout = findViewById(R.id.refreshlayout);
        mRefreshLayout.setOnRefreshLoadListener(this);
        android.support.v7.widget.Toolbar toolbar = findViewById(R.id.toolbar);
        toolbar.setTitle("Widgets");
        setSupportActionBar(toolbar);
        mViewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            int prev = -1;

            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                if (prev > -1) {
                    final MyListFragment prevFragment = mFragments[prev];
                    prevFragment.cancel();
                    mRefreshLayout.setFinished();
                }
                prev = position;

//                requestRefresh(position);
                mRefreshLayout.autoRefresh();

            }

        });

    }

    private void requestRefresh(int page) {
        final MyListFragment currFragment = mFragments[page];
        currFragment.doRefreshing();
    }

    private void requestLoading(int page) {
        final MyListFragment currFragment = mFragments[page];
        currFragment.doLoading();
    }


    public static void gotoActivity(Context context) {
        Intent intent = new Intent(context, RefreshActivity.class);
        context.startActivity(intent);
    }

    @Override
    public void onRefresh(CustomRefreshLayout view) {
        requestRefresh(mViewPager.getCurrentItem());
    }

    @Override
    public void onLoading(CustomRefreshLayout view) {
        requestLoading(mViewPager.getCurrentItem());
    }


    private static class ViewPagerAdapter extends FragmentPagerAdapter {

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return String.valueOf(position);
        }

        MyListFragment[] listFragments;

        public ViewPagerAdapter(FragmentManager fm, MyListFragment[] listFragments) {
            super(fm);
            this.listFragments = listFragments;
        }

        @Override
        public Fragment getItem(int position) {
            return listFragments[position];
        }

        @Override
        public int getCount() {
            return listFragments.length;
        }
    }


    public static class MyListFragment extends Fragment {
        private RecyclerView mRecyclerView;
//        private CoustomSwipeRefreshLayout mySwipeRefreshLayout;

        //        private MySwipeRefreshLayout customRefreshLayout;
        SimpleAdapter mAdapter;
        private final Handler mHandler = new Handler();

        @Nullable
        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            return inflater.inflate(R.layout.fragment_refresh, container, false);
        }

        void doRefreshing() {
            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
//                    customRefreshLayout.setRefreshEnabled(false);

                    final String[] strings = new String[50];
                    for (int i = 0; i < strings.length; ++i) {
                        strings[i] = String.valueOf(i);
                    }


                    mAdapter.refreshDataArrayWithHasNextPage(strings);
                    ((RefreshActivity) getActivity()).mRefreshLayout.setFinished(false);

//                    customRefreshLayout.setFinished(false);

                }
            }, 3000);
        }

        void doLoading() {
            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
//                    customRefreshLayout.setLoadEnabled(false);

                    final String[] strings = new String[20];
                    for (int i = 0; i < strings.length; ++i) {
                        strings[i] = String.valueOf(mAdapter.getItemCount() + i);
                    }
                    mAdapter.addDataArrayWithHasNextPage(strings);
                    ((RefreshActivity) getActivity()).mRefreshLayout.setFinished(false);


//                    customRefreshLayout.setFinished(false);
                }
            }, 3000);
        }

        void cancel() {
            mHandler.removeCallbacksAndMessages(null);
        }

        @Override
        public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);
            mRecyclerView = view.findViewById(R.id.recyclerview);
            mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
            mRecyclerView.setAdapter(mAdapter = new SimpleAdapter(null));
        }

        @Override
        public void onDestroyView() {
            mHandler.removeCallbacksAndMessages(null);
            super.onDestroyView();
        }


    }

    public static class SimpleAdapter extends BasePageAdapter<String, BaseRecyclerAdapter.BaseViewHolder> {

        public SimpleAdapter(List<String> data) {
            super(data);
        }


        @Override
        public BaseRecyclerAdapter.BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new BaseViewHolder(new TextView(parent.getContext()));
        }

        @Override
        public void onBindViewHolder(BaseRecyclerAdapter.BaseViewHolder holder, int position) {
            ((TextView) (holder.itemView)).setText(getItem(position));

        }

    }
}
